#include "TextureManager.h"



TextureManager:: TextureManager() {
}

TextureManager* TextureManager::_singleton = NULL;

void TextureManager::initialize(LPDIRECT3DDEVICE9 d3ddv) {
	if (_singleton == NULL) {
		_singleton = new TextureManager;
		// add resources1
		// menu resources
		_singleton->addTextureFromPath(d3ddv, _IMG_BGR_MENU, _ID_MENU, 800, 600, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_BGR_MENU_PUSHKEY, _MENU_PUSHKEY, 175, 21.5, 2, 1);
		// contra texture
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_LEG_STAND_RIGHT, _ID_CONTRA_LEG_STAND_RIGHT, 70, 76, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_LEG_MOVE_RIGHT, _ID_CONTRA_LEG_MOVE_RIGHT, 50, 75, 6, 6);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_MOVE_RIGHT, _ID_CONTRA_BODY_MOVE_RIGHT, 50, 75, 6, 6);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_FIRE_RIGHT, _ID_CONTRA_BODY_FIRE_RIGHT, 74, 80, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_UP_RIGHT, _ID_CONTRA_BODY_UP_RIGHT, 74, 110, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_UP_CROSS_RIGHT, _ID_CONTRA_BODY_UP_CROSS_RIGHT, 74, 110, 4, 4);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_FIRE_UP_CROSS_RIGHT, _ID_CONTRA_BODY_FIRE_UP_CROSS_RIGHT, 74, 110, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_DOWN_CROSS_RIGHT, _ID_CONTRA_BODY_DOWN_CROSS_RIGHT, 52, 75, 4, 4);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_FIRE_DOWN_CROSS_RIGHT, _ID_CONTRA_BODY_FIRE_DOWN_CROSS_RIGHT, 52, 75, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_FIRE_UP_RIGHT, _ID_CONTRA_BODY_FIRE_UP_RIGHT, 74, 110, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_STAND_RIGHT, _ID_CONTRA_BODY_STAND_RIGHT, 74, 80, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_LEG_STAND_LEFT, _ID_CONTRA_LEG_STAND_LEFT, 70, 76, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_LEG_MOVE_LEFT, _ID_CONTRA_LEG_MOVE_LEFT, 50, 80, 6,6);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_MOVE_LEFT, _ID_CONTRA_BODY_MOVE_LEFT, 50, 80, 6, 6);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_FIRE_LEFT, _ID_CONTRA_BODY_FIRE_LEFT, 74, 80, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_FIRE_UP_LEFT, _ID_CONTRA_BODY_FIRE_UP_LEFT, 74, 110, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_UP_LEFT, _ID_CONTRA_BODY_UP_LEFT, 74, 110, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_UP_CROSS_LEFT, _ID_CONTRA_BODY_UP_CROSS_LEFT, 74, 110, 4, 4);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_FIRE_UP_CROSS_LEFT, _ID_CONTRA_BODY_FIRE_UP_CROSS_LEFT, 74, 110, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_DOWN_CROSS_LEFT, _ID_CONTRA_BODY_DOWN_CROSS_LEFT, 50, 75, 4, 4);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_FIRE_DOWN_CROSS_LEFT, _ID_CONTRA_BODY_FIRE_DOWN_CROSS_LEFT, 50, 75, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_STAND_LEFT, _ID_CONTRA_BODY_STAND_LEFT, 74, 80, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_LEG_SIT_LEFT, _ID_CONTRA_LEG_SIT_LEFT, 90, 80, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_LEG_FIRE_SIT_LEFT, _ID_CONTRA_LEG_FIRE_SIT_LEFT, 90, 80, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_LEG_SIT_RIGHT, _ID_CONTRA_LEG_SIT_RIGHT, 90, 80, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_LEG_FIRE_SIT_RIGHT, _ID_CONTRA_LEG_FIRE_SIT_RIGHT, 90, 80, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_SIT_LEFT, _ID_CONTRA_BODY_SIT_LEFT, 90, 80, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_FIRE_SIT_LEFT, _ID_CONTRA_BODY_FIRE_SIT_LEFT, 90, 80, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_SIT_RIGHT, _ID_CONTRA_BODY_SIT_RIGHT, 90, 80, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_FIRE_SIT_RIGHT, _ID_CONTRA_BODY_FIRE_SIT_RIGHT, 90, 80, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_FLY_RIGHT, _ID_CONTRA_BODY_FLY_RIGHT, 52, 75, 4, 4);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_FLY_LEFT, _ID_CONTRA_BODY_FLY_LEFT, 52, 75, 4, 4);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_LEG_FLY_RIGHT, _ID_CONTRA_LEG_FLY_RIGHT, 52, 75, 4, 4);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_LEG_FLY_LEFT, _ID_CONTRA_LEG_FLY_LEFT, 52, 75, 4, 4);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_SWIM_LEFT, _ID_CONTRA_BODY_SWIM_LEFT, 40, 40,1 ,1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_SWIM_RIGHT, _ID_CONTRA_BODY_SWIM_RIGHT, 40, 40,1 ,1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_SWIM_LEFT, _ID_CONTRA_BODY_SWIM_LEFT, 40, 40,1 ,1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_SWIM_RIGHT_FIRE, _ID_CONTRA_BODY_SWIM_RIGHT_FIRE, 60, 40,1 ,1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_SWIM_LEFT_FIRE, _ID_CONTRA_BODY_SWIM_LEFT_FIRE, 60, 40,1 ,1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_SWIM_DOWN, _ID_CONTRA_BODY_SWIM_DOWN, 38, 30,1 ,1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_SWIM_FIRE_UP_LEFT, _ID_CONTRA_BODY_SWIM_FIRE_UP_LEFT, 51, 80,2 ,2);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_SWIM_FIRE_UP_RIGHT, _ID_CONTRA_BODY_SWIM_FIRE_UP_RIGHT, 51, 80,2 ,2);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_SWIM_FIRE_CROSS_LEFT, _ID_CONTRA_BODY_SWIM_FIRE_CROSS_LEFT, 60, 40,1 ,1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_BODY_SWIM_FIRE_CROSS_RIGHT, _ID_CONTRA_BODY_SWIM_FIRE_CROSS_RIGHT, 60, 40,1 ,1);
		_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_DEAD, _ID_CONTRA_DEAD, 80, 50,5 ,5);
		_singleton->addTextureFromPath(d3ddv, _IMG_ICON_LIFE, _ID_ICON_LIFE, 20, 40, 1, 1); 
		//_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_SIT_FIRE_LEFT, _ID_CONTRA_SIT_FIRE_LEFT, 90, 45, 2, 2);
		//_singleton->addTextureFromPath(d3ddv, _IMG_CONTRA_SIT_FIRE_RIGHT, _ID_CONTRA_SIT_FIRE_RIGHT, 90, 45, 2, 2);

		// Obstruction 
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE1_GRASS_1, _ID_MAP_STAGE1_GRASS_1, 64, 26, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE1_WATER_1, _ID_MAP_STAGE1_WATER_1, 64, 64, 8, 4);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE1_BRIDGE_1, _ID_MAP_STAGE1_BRIDGE_1, 160, 96, 29, 4);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE1_BRIDGE_LEFT_1, _ID_MAP_STAGE1_BRIDGE_LEFT_1, 54, 62, 3,3);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE1_BRIDGE_CENTER_1, _ID_MAP_STAGE1_BRIDGE_CENTER_1, 64, 62, 3, 3);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE1_BRIDGE_RIGHT_1, _ID_MAP_STAGE1_BRIDGE_RIGHT_1, 74, 62, 2, 2);
		
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE2_STONE_1, _ID_MAP_STAGE2_STONE_1, 48, 54, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE2_BOAT_1, _ID_MAP_STAGE2_BOAT_1, 64, 62, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE2_BRIDGE_1, _ID_MAP_STAGE2_BRIDGE_1, 256, 28, 2, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE2_WATER_ENEMY_1, _ID_MAP_STAGE2_WATER_ENEMY_1, 34, 31, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE2_WATER_ENEMY_2, _ID_MAP_STAGE2_WATER_ENEMY_2, 30, 62, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE2_FIRE, _ID_MAP_STAGE2_FIRE, 32, 32, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE3_BOOM_1, _ID_MAP_STAGE3_BOOM_1, 28, 28, 8, 8);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE3_GUN_ENEMY_1, _ID_MAP_STAGE3_GUN_ENEMY_1, 66, 64, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE3_TANK_1, _ID_MAP_STAGE3_TANK_1, 192, 130, 24, 6);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE3_BOSS_1, _ID_MAP_STAGE3_BOSS_1, 120, 128, 10, 10);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE3_BOSS_ITEM_1, _ID_MAP_STAGE3_BOSS_ITEM_1, 52, 36, 3, 1);
		// Map tiles
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE1_BGR_TILES, _ID_MAP_STAGE1_BGR_TILES, 34, 32, 0, 0);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE2_BGR_TILES, _ID_MAP_STAGE2_BGR_TILES, 34, 32, 0, 0);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE3_BGR_TILES, _ID_MAP_STAGE3_BGR_TILES, 34, 32, 0, 0);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE1_BGR_TILES_SPRITE, _ID_MAP_STAGE1_BGR_TILES_SPRITE, 34, 32, 0, 0);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE2_BGR_TILES_SPRITE, _ID_MAP_STAGE2_BGR_TILES_SPRITE, 34, 32, 0, 0);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE3_BGR_TILES_SPRITE, _ID_MAP_STAGE3_BGR_TILES_SPRITE, 34, 32, 0, 0);

		// Army
		_singleton->addTextureFromPath(d3ddv, _IMG_ARMY_GUN_2, _ID_ARMY_GUN_2, 68, 68, 21, 7);
		_singleton->addTextureFromPath(d3ddv, _IMG_ARMY_GUN_1, _ID_ARMY_GUN_1, 70, 70, 16, 8);
		_singleton->addTextureFromPath(d3ddv, _IMG_ARMY_GUN_1_EFFECT, _ID_ARMY_GUN_1_EFFECT, 70, 70, 16, 8);
		_singleton->addTextureFromPath(d3ddv, _IMG_ARMY_GUN_1_BEFORE, _ID_ARMY_GUN_1_BEFORE, 70, 70, 3, 3);
		_singleton->addTextureFromPath(d3ddv, _IMG_ARMY_RED_SOLDIER, _ID_ARMY_RED_SOLDIER, 80, 80, 10, 5);
		_singleton->addTextureFromPath(d3ddv, _IMG_ARMY_RED_HIDDEN, _ID_ARMY_RED_HIDDEN, 80, 80, 7, 7);
		_singleton->addTextureFromPath(d3ddv, _IMG_ARMY_RUNNING_SOLDIER, _ID_ARMY_RUNNING_SOLDIER, 60, 70, 6, 6);
		_singleton->addTextureFromPath(d3ddv, _IMG_ARMY_BOSS_TOP_LEFT_1, _ID_ARMY_BOSS_TOP_LEFT_1, 24, 12, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_ARMY_BOSS_TOP_RIGHT_1, _ID_ARMY_BOSS_TOP_RIGHT_1, 46, 20, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_ARMY_BOSS_BOTTOM_1, _ID_ARMY_BOSS_BOTTOM_1, 214, 122, 3, 3);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE3_BOSS_DOOR_1, _ID_MAP_STAGE3_BOSS_DOOR_1, 192, 181, 3, 3);
		_singleton->addTextureFromPath(d3ddv, _IMG_ARMY_BOSS_2, _ID_ARMY_BOSS_2, 512, 384, 9, 3);
		_singleton->addTextureFromPath(d3ddv, _IMG_ARMY_BOSS_ITEM_2, _ID_ARMY_BOSS_ITEM_2, 34, 34, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_ARMY_BOSS_ITEM_TOP_2, _ID_ARMY_BOSS_ITEM_TOP_2, 34, 34, 1, 1);

		// Item
		_singleton->addTextureFromPath(d3ddv, _IMG_ITEM_1, _ID_ITEM_1, 60, 40, 10, 10);
		_singleton->addTextureFromPath(d3ddv, _IMG_ITEM_BLOCK, _ID_ITEM_BLOCK, 68, 75, 7, 7);

		// Bullet
		_singleton->addTextureFromPath(d3ddv, _IMG_BULLET_LEVEL_1, _ID_BULLET_LEVEL_1, 12, 12, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_BULLET_LEVEL_1, _ID_BULLET_TANK, 12, 12, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_BULLET_LEVEL_0, _ID_BULLET_LEVEL_0, 8, 8, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_BULLET_LEVEL_2, _ID_BULLET_LEVEL_2, 16, 16, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_BULLET_BOSS_1, _ID_BULLET_BOSS_1, 32, 32, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_BULLET_BOSS_1, _ID_BULLET_WATER_ARMY, 32, 32, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_BULLET_LEVEL_3, _ID_BULLET_LEVEL_3, 20, 20, 2, 2);
		_singleton->addTextureFromPath(d3ddv, _IMG_BULLET_LEVEL_3, _ID_BULLET_BOSS_3, 20, 20, 2, 2);

		//Effects
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE1_BRIDGE_FIER_1, _ID_MAP_STAGE1_BRIDGE_FIER_1, 160, 96, 29, 4);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE1_EFFECT_OBJECT_BURNING_1, _ID_MAP_STAGE1_OBJECT_BURNING_1, 64, 64, 3, 3);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE1_EFFECT_OBJECT_FIRE_1, _ID_MAP_STAGE1_OBJECT_FIRE_1, 60, 60, 3, 3);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE1_EFFECT_BULLET_BIG_FIRE_1, _ID_MAP_STAGE1_EFFECT_BULLET_BIG_FIRE_1, 30, 30, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE1_EFFECT_BULLET_SMALL_FIRE_1, _ID_MAP_STAGE1_EFFECT_BULLET_SMALL_FIRE_1, 16, 16, 1, 1);
		_singleton->addTextureFromPath(d3ddv, _IMG_MAP_STAGE1_EFFECT_WATER_FALL_1, _ID_MAP_STAGE1_EFFECT_WATER_FALL_1, 28, 28, 1, 1);
	}
}

TextureManager* TextureManager::getInstance(LPDIRECT3DDEVICE9 d3ddv) {
	initialize(d3ddv);
	return _singleton;
}

Texture* TextureManager::getTexture(int filename) {
	if (_singleton->_mapTextures.find(filename) == _mapTextures.end()) {
		return NULL;
	}
	return _singleton->_mapTextures[filename];
}

void TextureManager::addTextureFromPath(LPDIRECT3DDEVICE9 d3ddv, LPWSTR filepath, int filename, int Width, int Height, int Count, int PerRow) {
	Texture *texture = Texture::createTextureFromFilePath(d3ddv, filepath, Width, Height, Count, PerRow);	
	_mapTextures.insert(make_pair(filename, texture));
}

void TextureManager::destroy() {
	map<int, Texture*>::iterator it;
	for (it = _mapTextures.begin(); it != _mapTextures.end(); it++)  {
		it->second->destroy();
		free(it->second);
	}
}
